package org.nf.trading.controller.app.product;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.trading.controller.BaseController;
import org.nf.trading.dao.ProductMapper;
import org.nf.trading.dto.ProductCommentDTO;
import org.nf.trading.entity.Product;
import org.nf.trading.service.app.product.impl.ProductServiceImpl;
import org.nf.trading.service.es.document.product.ProductDoc;
import org.nf.trading.vo.ResultVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api/product")
@CrossOrigin
public class ProductController extends BaseController {
    private final ProductServiceImpl productService;
    private final ProductMapper mapper;

    /**
     *  通过商品id查询，包括用户信息和评价：改良版
     * @param productId
     * @return
     */
    @GetMapping("/second/{productId}")
    public ProductCommentDTO test(@PathVariable("productId") Integer productId){
        ProductCommentDTO seconds = productService.secondLevelComments(productId);
        return seconds;
    }

    /**
     * 通过卖家id查询全部商品
     * @param uid
     * @return
     */
    @GetMapping("/uid/{uid}")
    public ResultVO listProductsByUid(@PathVariable("uid") Integer uid){
        return success(productService.listProductsByUid(uid));
    }

    /**
     * 商品图片上传
     * @param path
     * @param files
     * @return
     */
    @PostMapping("/upload/{path}")
    public ResultVO fileUpload(@PathVariable("path")String path, MultipartFile[] files){
        boolean result = productService.fileUpload(path, files,null);
        return success(200, "上传成功", result);
    }

    /**
     * 通过名称和描述查询（分页）
     * @param productName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/productName/{productName}/pageNum/{pageNum}/pageSize/{pageSize}")
    public ResultVO listProductsByName(
            @PathVariable("productName") String productName,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize){
        List<ProductDoc> productDocList = productService.getProductByName(productName,pageNum,pageSize);
        return success(productDocList);
    }

    /**
     * 商品发布
     * @param product
     * @return
     */
    @PostMapping("/product")
    public ResultVO addProduct(Product product,MultipartFile[] files){
        boolean saveProduct = productService.saveProduct(product,files);
        return success(200,"发布成功",saveProduct);
    }

    /**
     * 通过商品状态查询全部商品
     * @param status
     * @return
     */
    @GetMapping("/productStatus/{status}")
    public ResultVO getProductByStatus(@PathVariable("status") Integer status){
        List<Product> productList = productService.getProductByStatus(status);
        return success(productList);
    }
    /**
     * 通过商品id查询，包括用户信息和评价
     * @param productId
     * @return
     */
    @GetMapping("/{productId}")
    public ResultVO getProductById(@PathVariable("productId") Integer productId){
        ProductCommentDTO product = productService.getProductById(productId);
        System.out.println("product = " + product);
        return success(product);
    }

    /**
     * 查询全部商品
     * @return
     */
    @GetMapping
    public ResultVO selectAllProduct(Integer pageNum,Integer pageSize){
        PageInfo<Product> pageInfo = productService.selectAll(pageNum, pageSize);
        return success(pageInfo);
    }

    /**
     * 查询全部商品 + 多条件
     * @return
     */
    @GetMapping("/selectAllByProduct")
    public ResultVO selectAllByProduct(Integer pageNum,Integer pageSize,Product product){
        PageInfo<Product> pageInfo = productService.selectAllProduct(pageNum, pageSize,product);
        return success(pageInfo);
    }

    /**
     * 通过商品分类详细id查询全部商品
     * @param productTypeDetailId
     * @return
     */
    @GetMapping("/productTypeDetailId/{productTypeDetailId}")
    public ResultVO getProductByTypeDetailId(@PathVariable("productTypeDetailId") Integer productTypeDetailId){
        List<Product> productList = productService.getProductByTypeDetailId(productTypeDetailId);
        return success(productList);
    }

    @PostMapping("/updateStatus/{pid}/{status}")
    public ResultVO updateStatus(@PathVariable("pid") Integer pid,@PathVariable("status") Integer status) {
        mapper.updateProductStatus(pid,status);
        ResultVO vo = success();
        return vo;
    }

    /**
     * 查询某商品的评论数
     * @param pid
     * @return
     */
    @GetMapping("/productCommentNum/{pid}")
    public ResultVO productCommentNum(@PathVariable("pid") Integer pid){
        AtomicLong atomicLong = productService.productCommentNum(pid);
        ResultVO vo = new ResultVO();
        vo.setData(atomicLong);
        return vo;
    }

    @PostMapping("/delProduct/{pid}")
    public ResultVO delProduct(@PathVariable("pid") Integer pid) {
        mapper.deleteByPrimaryKey(pid);
        return success();
    }

}
